package com.kavin.design.adapter;//模拟一个DispatcherServlet
import java.util.ArrayList;  
import java.util.List;  
  
 /**
  * @Date 2020/6/28
  * @param  * @param null
  * @return
  * @Description 适配器模式
 */

public class DispatchServlet {  
      
    public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();   
      
    public DispatchServlet(){  
        handlerAdapters.add(new AnnotationHandlerAdapter());  
        handlerAdapters.add(new HttpHandlerAdapter());  
        handlerAdapters.add(new SimpleHandlerAdapter());  
    }  
      
      
    public void doDispatch(Controller controller) throws Exception {
        HandlerAdapter adapter = getHandler(controller);  
        //通过适配器执行对应的controller对应方法  
        adapter.handle(controller);  
          
    }  

    /**
     * @Date 2020/6/28
     * @param  * @param controller
     * @return com.kavin.design.adapter.HandlerAdapter
     * @Description 获取适配器
    */

    public HandlerAdapter getHandler(Controller controller) throws Exception {
        for(HandlerAdapter adapter: this.handlerAdapters){  
            if(adapter.supports(controller)){  
                return adapter;  
            }  
        }  
       throw new Exception("not Supports Controller Type");
    }  
      
    public static void main(String[] args) throws Exception {
        new DispatchServlet().doDispatch(new SimpleController());
    }  
      
} 